iT邦幫忙

2025 iThome 鐵人賽

DAY 11
1

牧場主今日工作

昨天我們成功部署了 Rancher Server,但要讓整個數位牧場運作順暢,還需要把各個基礎設施串聯起來!今天我們要完善 PowerDNS 的內外部解析、設定 HAProxy 的多域名路由,以及優化 Rancher 的安全設定。這就像是為牧場建立完整的道路系統和安全管理制度,讓所有服務都能互相溝通!

PowerDNS 內外部解析設定

安裝並設定 PowerDNS Recursor

PowerDNS Authoritative Server 無法直接處理遞迴查詢,需要安裝 PowerDNS Recursor:

# SSH 到 bastion 節點
ssh calvin@192.168.0.135

# 安裝 PowerDNS Recursor
sudo apt update
sudo apt install -y pdns-recursor

# 修改 PowerDNS 主服務端口避免衝突
sudo vim /etc/powerdns/pdns.conf
# 加入:
local-port=5300

# 重啟 PowerDNS
sudo systemctl restart pdns

# 編輯 Recursor 配置
sudo vim /etc/powerdns/recursor.conf
# 主要設定:
local-address=192.168.0.135
local-port=53
allow-from=127.0.0.0/8,192.168.0.0/16
forward-zones=ithome-rancher.duckdns.org.=127.0.0.1:5300
forward-zones-recurse=.=8.8.8.8;8.8.4.4
dnssec=off

# 啟動並設定開機啟動
sudo systemctl enable pdns-recursor
sudo systemctl start pdns-recursor

# 設定本機使用新的 DNS
sudo vim /etc/resolv.conf
# 修改為:
nameserver 192.168.0.135

# 測試解析
nslookup google.com
nslookup ithome-rancher.duckdns.org

修復 PowerDNS 資料庫結構

如果遇到資料庫結構問題,需要先修復:

# 修復 MariaDB 資料庫結構
mysql -u pda -p pda

-- 添加缺少的欄位
ALTER TABLE domains ADD COLUMN options VARCHAR(64000) DEFAULT NULL;
ALTER TABLE domains ADD COLUMN catalog VARCHAR(255) DEFAULT NULL;

-- 修正 type 欄位長度
ALTER TABLE domains MODIFY COLUMN type VARCHAR(8) NOT NULL;

-- 檢查修正結果
DESCRIBE domains;

EXIT;

新增內部域名解析

建立專用的域名區域來解析 Rancher 服務:

# 建立 ithome-rancher.duckdns.org 區域
sudo pdnsutil create-zone ithome-rancher.duckdns.org

# 設定 SOA 記錄
sudo pdnsutil replace-rrset ithome-rancher.duckdns.org @ SOA \
"ns1.ithome-rancher.duckdns.org. admin.ithome-rancher.duckdns.org. 1 3600 600 604800 300"

# 設定 NS 記錄和 glue 記錄
sudo pdnsutil add-record ithome-rancher.duckdns.org @ NS ns1.ithome-rancher.duckdns.org
sudo pdnsutil add-record ithome-rancher.duckdns.org ns1 A 127.0.0.1

# 設定內網 A 記錄(指向 bastion,透過 HAProxy 處理)
sudo pdnsutil add-record ithome-rancher.duckdns.org @ A 192.168.0.135

# 檢查設定結果
sudo pdnsutil list-zone ithome-rancher.duckdns.org

# 驗證記錄
nslookup ithome-rancher.duckdns.org localhost

設定所有節點使用 bastion DNS

我們需要讓所有 Rancher 節點都使用 bastion 作為 DNS 伺服器:

# 在每個節點上執行(rancher-server-1, rancher-master-1, rancher-worker-1)
sudo apt update
sudo apt install -y vim dnsutils
# 1. 編輯 systemd-resolved 設定
sudo vim /etc/systemd/resolved.conf

# 修改以下行:
DNS=192.168.0.135

# 2. 重新啟動 DNS 服務
sudo systemctl restart systemd-resolved

# 3. 驗證 DNS 設定
resolvectl status
nslookup ithome-rancher.duckdns.org

HAProxy 多域名路由設定

基本 HAProxy 配置

# SSH 到 bastion 節點
ssh calvin@192.168.0.135

# 備份現有配置
sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.backup

# 建立新的 HAProxy 配置
sudo tee /etc/haproxy/haproxy.cfg << 'EOF'
global
    log stdout local0
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin
    stats timeout 30s
    user haproxy
    group haproxy
    daemon

defaults
    mode http
    log global
    option httplog
    option dontlognull
    option log-health-checks
    timeout connect 5000
    timeout client 50000
    timeout server 50000
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http

# HTTP 前端(重導向到 HTTPS)
frontend http_frontend
    bind *:80
    redirect scheme https code 301

# HTTPS 前端
frontend https_frontend
    bind *:443 ssl crt /etc/ssl/certs/ithome-rancher/ithome-rancher.pem
    option forwardfor
    
    # 基於 Host Header 的路由
    acl rancher_host hdr(host) -i ithome-rancher.duckdns.org
    
    # 路由規則
    use_backend rancher_backend if rancher_host
    
    # 預設後端
    default_backend rancher_backend

# Rancher 後端
backend rancher_backend
    balance roundrobin
    option httpchk GET /healthz
    http-check expect status 200
    server rancher-server-1 192.168.0.116:443 ssl verify none check
EOF

# 測試配置語法
sudo haproxy -c -f /etc/haproxy/haproxy.cfg

# 重新啟動 HAProxy
sudo systemctl restart haproxy
sudo systemctl status haproxy

SSL 憑證整合

# 建立 SSL 憑證組合(如果還沒建立)
sudo mkdir -p /etc/ssl/certs/ithome-rancher

# 組合 fullchain 和 privkey(HAProxy 格式)
sudo cat /etc/letsencrypt/live/ithome-rancher.duckdns.org/fullchain.pem \
     /etc/letsencrypt/live/ithome-rancher.duckdns.org/privkey.pem \
     > /tmp/ithome-rancher.pem

sudo mv /tmp/ithome-rancher.pem /etc/ssl/certs/ithome-rancher/ithome-rancher.pem
sudo chmod 600 /etc/ssl/certs/ithome-rancher/ithome-rancher.pem
sudo chown root:root /etc/ssl/certs/ithome-rancher/ithome-rancher.pem

# 重新載入 HAProxy
sudo systemctl reload haproxy

測試多域名路由

# 測試 HTTP 重導向
curl -I http://ithome-rancher.duckdns.org

Rancher 安全性優化設定

修改 agent-tls-mode 設定

為什麼要改為 system-store?

  • 預設模式 (strict):Rancher agent 只信任 Rancher 管理的憑證
  • system-store 模式:使用系統憑證庫,支援企業內部 CA 和自簽憑證
  • 適用場景:當使用 HAProxy SSL 終止時,後端連線可能使用不同憑證

透過 Rancher UI 設定:

  1. 登入 Rancher 管理介面
  2. 前往 Global Settings
  3. 找到 agent-tls-mode 設定
  4. 將值從 strict 改為 system-store
  5. 點擊 Save

設定 kubeconfig token 永不過期

為什麼要設定 TTL 為 0?

  • 預設行為:Rancher 產生的 kubeconfig token 有時間限制
  • 設為 0:表示 token 永不過期,適合實驗和開發環境
  • 注意:生產環境建議保留預設值以增強安全性

透過 Rancher UI 設定:

  1. 登入 Rancher 管理介面
  2. 前往 Global Settings
  3. 找到 kubeconfig-default-token-ttl-minutes 設定
  4. 將值改為 0
  5. 點擊 Save

本機開發環境設定

修改 MacBook 的 hosts 檔案

現在 HAProxy 已經配置完成,我們需要將本機的域名解析指向 bastion 節點:

# 在 MacBook 上編輯 hosts 檔案
sudo vim /etc/hosts

# 修改或新增以下行:
192.168.0.135 ithome-rancher.duckdns.org

# 驗證解析
ping -c 3 ithome-rancher.duckdns.org

# 建議不要加 PowerDNS 到 MacBook 的 DNS,離開網路就連不上了
# 寫 /etc/hosts 是比較穩定的做法

為什麼要改為 bastion IP?

  • 之前指向 rancher-server-1 (192.168.0.116) 會繞過 HAProxy
  • 現在指向 bastion (192.168.0.135) 讓流量經過 HAProxy 處理
  • HAProxy 負責 SSL 終止和負載均衡

瀏覽器驗證測試

完整的瀏覽器測試:

  1. 開啟瀏覽器 前往 https://ithome-rancher.duckdns.org
  2. 檢查 SSL 狀態:位址列應該顯示鎖頭圖示,不會顯示「不安全」警告
  3. 驗證憑證:點擊鎖頭圖示,確認憑證由 Let's Encrypt 簽發
  4. 登入 Rancher:使用管理員帳號密碼登入
  5. 測試功能
    • 查看 local 叢集狀態
    • 下載 kubeconfig(token 永不過期)
    • 檢查 Global Settings 中的 agent-tls-mode 和 token TTL 設定

預期結果:

  • ✅ 瀏覽器顯示安全連線
  • ✅ Let's Encrypt 有效憑證
  • ✅ Rancher 管理介面正常載入
  • ✅ 所有設定已正確套用

今日總結與明日預告

今天我們完成了基礎設施的全面整合!PowerDNS 現在能正確解析內外部域名,HAProxy 可以根據域名路由流量,Rancher 也有了更安全的配置設定。

重點回顧:

  • 設定 PowerDNS 的內部記錄和 upstream 轉發
  • 配置所有節點使用統一的 DNS 服務
  • 建立 HAProxy 多域名路由機制
  • 整合 SSL 憑證到負載均衡器
  • 優化 Rancher 的 TLS 和 token 設定
  • 完成端到端的整合驗證

明天我們將開始建立 Custom Cluster,學習如何透過 Rancher 管理多個 Kubernetes 叢集,真正體驗多叢集管理的威力!


💡 牧場主小提示:DNS 設定生效可能需要一些時間,如果遇到解析問題先檢查 systemd-resolved 的狀態!另外,HAProxy 的 SSL 憑證路徑要確保正確,否則 HTTPS 會失敗。記得定期檢查憑證到期時間並更新!


上一篇
Day 10: Rancher Server 華麗登場
下一篇
Day 12: Custom Cluster 魔法召喚
系列文
牧場主的 K8s 放牧日記21
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言